package com.agricultural.machine.user;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 用户会话类
 */



public class UserSession {
    private final String sessionId;
    private final String username;
    private final UserRole role;
    private LocalDateTime lastAccessTime;
    private boolean active;
    
    public UserSession(String username, UserRole role) {
        this.sessionId = UUID.randomUUID().toString();
        this.username = username;
        this.role = role;
        this.lastAccessTime = LocalDateTime.now();
        this.active = true;
    }
    
    public String getSessionId() {
        return sessionId;
    }
    
    public String getUsername() {
        return username;
    }
    
    public UserRole getRole() {
        return role;
    }
    
    public LocalDateTime getLastAccessTime() {
        return lastAccessTime;
    }
    
    public void updateLastAccessTime() {
        this.lastAccessTime = LocalDateTime.now();
    }
    
    public boolean isActive() {
        return active;
    }
    
    public void setActive(boolean active) {
        this.active = active;
    }
    
    /**
     * 检查会话是否过期
     */
    public boolean isExpired(int timeoutMinutes) {
        return LocalDateTime.now().minusMinutes(timeoutMinutes).isAfter(lastAccessTime);
    }
    
    @Override
    public String toString() {
        return "UserSession{" +
                "sessionId='" + sessionId + '\'' +
                ", username='" + username + '\'' +
                ", role=" + role +
                ", lastAccessTime=" + lastAccessTime +
                ", active=" + active +
                '}';
    }
} 